﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace teavch.Util.RssFeedator
{
	public static class Helpers
	{
		public static string FeedatorRss( this HtmlHelper helper )
		{
			List<Rss> allRss = Feedator.CreateRss();
			System.Text.StringBuilder sb = new System.Text.StringBuilder();

			foreach ( Rss rss in allRss )
			{
				if ( rss != null )
				{
					sb.Append( "<div>" );
					sb.Append( "<p>" + rss.Channel.Title + "</p>" );
					sb.Append( "<p>" + rss.Channel.Description + "</p>" );
					foreach ( ChannelContent item in rss.Channel.ChannelContent )
					{
						sb.Append( "<p>" + item.Title + "</p>" );
						if ( item.Description.Text.Contains( "<div" ) )
							sb.Append( item.Description );
						else
							sb.Append( "<p>" + item.Description + "</p>" );
						sb.Append( "<p>Changed by " + item.Author + " on " + item.PublishDate + "</p>" );
					}
					sb.Append( "</div>" );
				}
			}

			return sb.ToString();
		}
		public static string FeedatorRss( this HtmlHelper helper, Uri FeedUri )
		{
			Rss rss = Feedator.CreateRss( FeedUri );
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			sb.Append( "<div>" );

			sb.Append( "<div>" );
			sb.Append( "<p class=\"heading\"><a target=\"_blank\" href=\"" + rss.Channel.Link.Text.Replace( "project/feeds/rss", "" ) + "\">" + rss.Channel.Title + "</a></p>" );
			sb.Append( "<p class=\"headingDesc\">" + rss.Channel.Description + "</p>" );
			sb.Append( "</div>" );

			foreach ( ChannelContent item in rss.Channel.ChannelContent )
			{
				sb.Append( "<div>" );
				sb.Append( "<p class=\"subheading\"><a target=\"_blank\" href=\"" + item.Link.Text + "\">" + item.Title + "</a></p>" );
				if ( item.Description.Text.Contains( "<div>" ) )
					sb.Append( item.Description );
				else
					sb.Append( "<p class=\"subheadingDesc\">" + item.Description + "</p>" );
				sb.Append( "<p class=\"smalltext\">Changed by " + item.Author + " on " + item.PublishDate + "</p>" );
				sb.Append( "</div>" );
			}

			sb.Append( "</div>" );


			return sb.ToString();
		}
	}
}
